/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */

package cn.com.ksplatform.modules.system.auth.rolemanager.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import cn.com.ksplatform.base.service.BaseService;
import cn.com.ksplatform.common.logger.Log;
import cn.com.ksplatform.modules.system.auth.buttonmanager.model.PlatButton;
import cn.com.ksplatform.modules.system.auth.rolemanager.model.PlatRole;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * ClassName:PlatRoleService <br/>
 * s Date: 2015年5月8日 下午4:28:49 <br/>
 * 
 * @author Mr.Luoxing
 * @version 1.0
 */
@Repository("system.modules.system.PlatRoleService")
public class PlatRoleService extends BaseService {
	
	@Autowired
	@Qualifier("cacheManagerFactory")
	CacheManager cacheManager;
	
	@Transactional
	public PlatRole save(PlatRole role) {
		String id = role.save();
		role.setRoleId(id);
		return role;
	}

	@Transactional
	public PlatRole update(PlatRole role) {
		return role.update();
	}

	@Transactional
	public PlatRole delete(PlatRole role) {
		return role.delete();
	}

	@Transactional
	public PlatRole saveOrUpdate(PlatRole role) {
		return role.saveOrUpdate();
	}
	
	@Transactional
	public List<PlatRole> getAllRole(){
		return (List<PlatRole>) findAll("from PlatRole");
	}
	
	/**
	 * 刷新缓存
	 */
	@Transactional
	public int recache() {
		Cache cache = cacheManager.getCache("RoleDataCache");
		List<PlatRole> roles = getAllRole();
		int num = roles.size();
		for(PlatRole r : roles){
			Set b = r.getBtns(); 
			//Log.info("按钮数目:"+b.size());
			Set re = r.getResources();
			//Log.info("资源数目:"+re.size());
			Set m = r.getMenus();
			//Log.info("菜单数目:"+m.size());
			//Element btns = new Element(r.getRoleId()+"[buttons]", b);
			Element res = new Element(r.getRoleId()+"[resources]", re);
			Element menus = new Element(r.getRoleId()+"[menus]", m);
			//cache.put(btns);
			cache.put(res);
			cache.put(menus);
			
			//按钮分类（按照按钮组维度进行存储）
			buttonCache(cache,b,r.getRoleId()+"[buttons]");
		}
		return num;
	}
	
	public void buttonCache(Cache cache,Set<PlatButton> btns,String key){
		for(PlatButton btn : btns){
			Set<PlatButton> bs = new HashSet<PlatButton>();
			String bkey = key+"["+btn.getBtnGroup().getBtnGId()+"]";
			Element em = cache.get(bkey);
			if(em == null){
				em = new Element(key+"["+btn.getBtnGroup().getBtnGId()+"]",bs);
				cache.put(em);
			}else{
				bs = (Set<PlatButton>) cache.get(bkey).getObjectValue();
			}
			Log.info(bkey);
			bs.add(btn);
		}
	}

}
